﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RCT2
{
    public partial class SettingsEditor : Form
    {
        public SettingsEditor(Bars bars)
        {
            InitializeComponent();
            loadSettings();
            this.bars = bars;

            chkOneLeftAlign.DataBindings.Add("Checked", tempOneSettings, "leftAligned");
            numOneWarning.DataBindings.Add("Value", tempOneSettings, "percent0");
            numOneCritical.DataBindings.Add("Value", tempOneSettings, "percent1");
            cbBar1.DataBindings.Add("SelectedIndex", tempOneSettings, "varIndex");
            numOneTick.DataBindings.Add("Value", tempOneSettings, "timer");

            btnOneNormal.DataBindings.Add("BackColor", tempOneSettings, "colour0");
            btnOneWarning.DataBindings.Add("BackColor", tempOneSettings, "colour1");
            btnOneCritical.DataBindings.Add("BackColor", tempOneSettings, "colour2");

            chkTwoLeftAlign.DataBindings.Add("Checked", tempTwoSettings, "leftAligned");
            numTwoWarning.DataBindings.Add("Value", tempTwoSettings, "percent0");
            numTwoCritical.DataBindings.Add("Value", tempTwoSettings, "percent1");
            cbBar2.DataBindings.Add("SelectedIndex", tempTwoSettings, "varIndex");
            numTwoTick.DataBindings.Add("Value", tempTwoSettings, "timer");

            btnTwoNormal.DataBindings.Add("BackColor", tempTwoSettings, "colour0");
            btnTwoWarning.DataBindings.Add("BackColor", tempTwoSettings, "colour1");
            btnTwoCritical.DataBindings.Add("BackColor", tempTwoSettings, "colour2");
        }

        //When you change a value in the GUI, write it immediately to tempOne/TwoSettings so swap will work properly

        Bars bars;
        ColorDialog clrDialog = new ColorDialog();
        BarSettings tempOneSettings;
        BarSettings tempTwoSettings;

        private void btnSave_Click(object sender, EventArgs e)
        {
            Settings.oneSettings = (BarSettings)tempOneSettings.Clone();
            Settings.twoSettings = (BarSettings)tempTwoSettings.Clone();
            

            Settings.splitByHeight = radSBH.Checked;
            Settings.OptionsAtStartup = chkShowOptions.Checked;
            Settings.autoUpdate = chkAutoUpdate.Checked;
            Settings.fade = chkFadeColours.Checked;
            Settings.HideIcon = chkHideIcon.Checked;

            Settings.writeSettings();
            bars.needsUpdate = true;
            bars.setTimers();
        }

        void loadFromTemp()
        {
            chkOneLeftAlign.Checked = tempOneSettings.leftAligned;
            numOneWarning.Value = tempOneSettings.percents[0];
            numOneCritical.Value = tempOneSettings.percents[1];
            cbBar1.SelectedIndex = tempOneSettings.varIndex;
            numOneTick.Value = tempOneSettings.timer;
            btnOneNormal.BackColor = tempOneSettings.colours[0];
            btnOneWarning.BackColor = tempOneSettings.colours[1];
            btnOneCritical.BackColor = tempOneSettings.colours[2];

            chkTwoLeftAlign.Checked = tempTwoSettings.leftAligned;
            numTwoWarning.Value = tempTwoSettings.percents[0];
            numTwoCritical.Value = tempTwoSettings.percents[1];
            cbBar2.SelectedIndex = tempTwoSettings.varIndex;
            numTwoTick.Value = tempTwoSettings.timer;
            btnTwoNormal.BackColor = tempTwoSettings.colours[0];
            btnTwoWarning.BackColor = tempTwoSettings.colours[1];
            btnTwoCritical.BackColor = tempTwoSettings.colours[2];
        }

        void loadSettings()
        {
            Settings.readSettings();

            tempOneSettings = (BarSettings)Settings.oneSettings.Clone();
            tempTwoSettings = (BarSettings)Settings.twoSettings.Clone();

            loadFromTemp();

            radSBH.Checked = Settings.splitByHeight;
            chkShowOptions.Checked = Settings.OptionsAtStartup;
            chkAutoUpdate.Checked = Settings.autoUpdate;
            chkHideIcon.Checked = Settings.HideIcon;
            chkFadeColours.Checked = Settings.fade;            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            bars.checkUpdates(true);
        }

        public void changeLabel()
        {
            if (Settings.side == 0 || Settings.side == 2) { chkTwoLeftAlign.Text = "Left-aligned"; chkOneLeftAlign.Text = "Left-aligned"; }
            else { chkTwoLeftAlign.Text = "Bottom-aligned"; chkOneLeftAlign.Text = "Bottom-aligned"; }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0) this.Height = 220;
            else this.Height = 342;

            tabControl1.Invalidate();
        }

        private void btnColour_Click(object sender, EventArgs e)
        {
            clrDialog.Color = ((Button)sender).BackColor;
            if (clrDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                ((Button)sender).BackColor = clrDialog.Color;
        }

        private void btnDefaults_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you definitely sure?", "RCT2", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                Settings.oneSettings = new BarSettings(1000, new int[2] { 50, 75 }, new Color[3] { Color.CadetBlue, Color.Orange, Color.Red }, 1, false);
                Settings.twoSettings = new BarSettings(10000, new int[2] { 60, 80 }, new Color[3] { Color.LimeGreen, Color.Orange, Color.Red }, 0, false);

                Settings.memType = 1;
                Settings.splitByHeight = true;
                Settings.OptionsAtStartup = false;
                Settings.side = 0;
                Settings.fade = true;
                Settings.HideIcon = false;

                Settings.writeSettings();
                loadSettings();
                bars.needsUpdate = true;
                bars.setTimers();
            }
        }

        private void btnDefaultColors_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you definitely sure?", "RCT2", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                Settings.oneSettings.colours = new Color[3] { Color.CadetBlue, Color.Orange, Color.Red };
                Settings.twoSettings.colours = new Color[3] { Color.LimeGreen, Color.Orange, Color.Red };

                Settings.writeSettings();
                loadSettings();
                bars.needsUpdate = true;
            }
        }

        private void SettingsEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void btnSwap_Click(object sender, EventArgs e)
        {
            BarSettings temp = tempOneSettings;
            tempOneSettings = tempTwoSettings;
            tempTwoSettings = temp;
            loadFromTemp();
        }

        private void chkHideIcon_CheckedChanged(object sender, EventArgs e)
        {
            if (chkHideIcon.Checked)
                if (MessageBox.Show("To reenable the tray icon after it has been disabled, you will have to edit the settings.rcts file directly and restart the application.\r\nAre you sure you wish to hide the tray icon?", "RCT2", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                    chkHideIcon.Checked = false;
        }
    }
}